﻿#region zh-CHS 2006 - 2010 DemoSoft 团队 | en 2006-2010 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the MMOSE(Massively Multiplayer Online Server Engine) for .NET.
//
//                              2006-2010 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Demo.Mmose.Core.Common.SafeCollections;
#endregion

namespace Demo.Mmose.Core.Entity.Suit.Spell
{
    /// <summary>
    /// 技能容器（放在玩家身上）
    /// </summary>
    public class SpellCollection<SpellT> : IEnumerable<SpellT>
        where SpellT : Spell
    {
        #region zh-CHS 构造和初始化和清理 | en Constructors and Initializers and Dispose

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="spells"></param>
        public SpellCollection()
        {

        }

        #endregion

        #region zh-CHS 共有属性 | en Public Properties

        #region zh-CHS CoolDownTime属性 | en Public Properties

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 冷却咒语时间
        /// </summary>
        private TimeSpan m_CoolDownTime = TimeSpan.Zero;
        #endregion
        /// <summary>
        /// 冷却咒语时间
        /// </summary>
        public TimeSpan CoolDownTime
        {
            get { return m_CoolDownTime; }
        }

        /// <summary>
        /// 设置 冷却咒语时间
        /// </summary>
        public void SetNextCoolDownTime( TimeSpan coolDownTime )
        {
            // 每次设置都产生新的有效冷却的咒语时间

            m_NextCoolDownTime = DateTime.Now + m_CoolDownTime;
            m_CoolDownTime = coolDownTime;
        }

        #endregion

        #region zh-CHS NextCoolDownTime属性 | en Public Properties

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 冷却咒语时间
        /// </summary>
        private DateTime m_NextCoolDownTime = DateTime.MinValue;
        #endregion
        /// <summary>
        /// 下一次冷却咒语时间有效（总）
        /// </summary>
        public DateTime NextCoolDownTime
        {
            get { return m_NextCoolDownTime; }
        }

        #endregion

        #endregion

        #region zh-CHS 共有方法 | en Public Methods

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private SafeDictionary<long, SpellT> m_SpellDictionary = new SafeDictionary<long, SpellT>( 24 );
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <param name="spellId"></param>
        /// <param name="Spell"></param>
        public bool AddSpell( long spellId, SpellT Spell )
        {
            return m_SpellDictionary.TryAdd( spellId, Spell );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="spellId"></param>
        /// <param name="Spell"></param>
        public bool RemoveSpell( long spellId )
        {
            return m_SpellDictionary.Remove( spellId );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="spellId"></param>
        /// <returns></returns>
        public SpellT FindSpellOnId( long spellId )
        {
            SpellT spell = default( SpellT );
            m_SpellDictionary.TryGetValue( spellId, out spell );

            return spell;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="spellId"></param>
        /// <returns></returns>
        public void Clear()
        {
            m_SpellDictionary.Clear();
        }

        /// <summary>
        /// 技能时间有效
        /// </summary>
        public bool CheckSpellCoolDownTime( long spellId )
        {
            SpellT spell = null;
            if ( m_SpellDictionary.TryGetValue( spellId, out spell ) == false )
                return false;

            if ( spell == null )
                return false;

            DateTime nowDateTime = DateTime.Now;
            if ( nowDateTime < this.NextCoolDownTime )
                return false;

            if ( nowDateTime < spell.NextCoolDownTime )
                return false;

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public SpellT[] ToArray()
        {
            return m_SpellDictionary.ToArrayValues();
        }

        #endregion

        #region zh-CHS 接口实现 | en Interface Implementation
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerator<SpellT> GetEnumerator()
        {
            SpellT[] tempSpellArray = this.ToArray();
            if ( tempSpellArray == null )
                yield break;

            for ( int iIndex = 0; iIndex < tempSpellArray.Length; iIndex++ )
                yield return tempSpellArray[iIndex];
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
        #endregion
    }
}
#endregion